//
//  LivingGoodsViewController.m
//  PrintPort
//
//  Created by hy002 on 2017/12/28.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "LivingGoodsViewController.h"
#import "ProductModel.h"
#import "ImagelistModel.h"
#import "ProdTypeModel.h"
#import "LiveGoodsDetailViewController.h"

static NSString *cellPath = @"LiveGoodsCell";
static NSString *cellSortPath = @"LivesortCell";
static NSString * CellIdentifier = @"LivecollectCell";
@interface LivingGoodsViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
{
    UIButton *sortBtn;
    UIButton *salesBtn;
    UIView *backView;
}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UITableView *sortTableview;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *allGoodsAry;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, assign)NSInteger paixuType;
@property (nonatomic, assign)NSInteger rightType;
@property (nonatomic, strong)NSString *proID;
@property (nonatomic, assign)NSInteger closeType;
@property (nonatomic, strong)NSArray *leftAry;
@property (nonatomic, strong)NSArray *rightAry;


@end

@implementation LivingGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _paixuType = 0;
    _rightType = 0;
    _closeType = 0;
    _proID = @"";
    _leftAry = @[@"综合排序",@"销量优先",@"价格由高到低",@"价格由低到高"];
    _allGoodsAry = [NSMutableArray arrayWithCapacity:0];
    [self creatUI];
    [self requestLifeGoodsList:@"" ProductTypeId:_proID];
    [self requestLifeproductType];
    
}

- (void)creatUI
{
    self.navigationItem.title = @"商品列表";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30*displaywidth)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    CGFloat W = (SCREEN_WIDTH-30*displaywidth)/2;
    NSArray *titAry = @[@"综合排序",@"所有分类"];
    for (int i = 0; i < titAry.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor =[UIColor clearColor];
        button.frame = CGRectMake(W*i, 0, W, 30*displaywidth);
        [button setImage:[UIImage imageNamed:@"diankai"] forState:UIControlStateNormal];
        button.titleLabel.font = NormalFontWithSize(14.0+fontScale);
        [button setTitleColor:ThemeColor forState:UIControlStateNormal];
        [button setTitle:titAry[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closeBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10+i;
        CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:button.titleLabel.font}];
        CGSize imgSize = button.currentImage.size;
        UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
        UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
        button.titleEdgeInsets = titleEdgeInsets;
        button.imageEdgeInsets = imgEdgeInsets;
        button.selected = NO;
        [topView addSubview:button];
        if (i == 0)
        {
            sortBtn = button;
        }
        else
        {
            salesBtn = button;
        }
    }
    
    
    UIButton *changeBtn = [ZCControl createButtonWithFrame:CGRectMake(2*W, 0, 30*displaywidth, 30*displaywidth) ImageName:@"henglie" Target:self Action:@selector(changeBtnAction:) Title:nil Font:14.0];
    changeBtn.backgroundColor =[UIColor clearColor];
    changeBtn.selected = NO;
    [topView addSubview:changeBtn];
    UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(W, 0, 1, 30*displaywidth)];
    lineAview.backgroundColor = LinesColor;
    [topView addSubview:lineAview];
    UIView *lineBview = [[UIView alloc] initWithFrame:CGRectMake(2*W, 0, 1, 30*displaywidth)];
    lineBview.backgroundColor = LinesColor;
    [topView addSubview:lineBview];
    UIView *lineCview = [[UIView alloc] initWithFrame:CGRectMake(0, 29*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
    lineCview.backgroundColor = LinesColor;
    [topView addSubview:lineCview];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 30*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-30*displaywidth) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.tag = 11;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-30*displaywidth) collectionViewLayout:layout];
    _collectionView.backgroundColor = BacksColor;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    _collectionView.hidden = YES;
    [self.view addSubview:_collectionView];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 30*displaywidth, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-30*displaywidth)];
    backView.backgroundColor = RGBA(0, 0, 0, 0.6);
    [self.view addSubview:backView];
    self.sortTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120*displaywidth) style:UITableViewStylePlain];
    _sortTableview.backgroundColor = [UIColor whiteColor];
    _sortTableview.delegate = self;
    _sortTableview.dataSource = self;
    _sortTableview.tag = 22;
    _sortTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [backView addSubview:_sortTableview];
    backView.hidden = YES;
    
}


- (void)loadNewHeaderData
{
    _page = 0;
    NSString *sort = @"";
    if (_paixuType == 1)
    {
        sort = @"productSales,desc";
    }
    if (_paixuType == 2)
    {
        sort = @"price,desc";
    }
    else if (_paixuType == 3)
    {
        sort = @"price,asc";
    }
    
    
    [self requestLifeGoodsList:sort ProductTypeId:_proID];
}

- (void)loadMoreFootData
{
    _page ++;
    NSString *sort = @"";
    if (_paixuType == 1)
    {
        sort = @"productSales,desc";
    }
    if (_paixuType == 2)
    {
        sort = @"price,desc";
    }
    else if (_paixuType == 3)
    {
        sort = @"price.asc";
    }
    
    [self requestLifeGoodsList:sort ProductTypeId:_proID];
}

//切换
- (void)changeBtnAction:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.selected)
    {
        _tableview.hidden = YES;
        _collectionView.hidden = NO;
    }
    else
    {
        _tableview.hidden = NO;
        _collectionView.hidden = YES;
    }
}


- (void)closeBtnAct:(UIButton *)btn
{
    btn.selected = !btn.selected;
    _closeType = btn.tag - 10;
    NSString *imgStr = @"diankai";
    NSString *titStr = @"";
    if (btn.selected)
    {
        imgStr = @"guanbiclose";
    }
    if (btn.tag == 10)
    {
        //排序
        _sortTableview.frame = CGRectMake(0, 0, SCREEN_WIDTH, 120*displaywidth);
        titStr = _leftAry[_paixuType];
        NSString *rightStr = @"所有分类";
        if (_rightType != 0 && _rightAry.count > 0)
        {
            ProdTypeModel *model = _rightAry[_rightType-1];
            rightStr = model.typeName;
        }
        salesBtn.selected = NO;
        [self changeBtnTitleAndImage:salesBtn Imgstr:@"diankai" TitStr:rightStr];
    }
    else
    {
        //分类
        _sortTableview.frame = CGRectMake(0, 0, SCREEN_WIDTH, (_rightAry.count+1)*30*displaywidth);
        
        titStr = @"所有分类";
        
        if (_rightType != 0 && _rightAry.count > 0)
        {
            ProdTypeModel *model = _rightAry[_rightType-1];
            titStr = model.typeName;
        }
        NSString *leftStr =_leftAry[_paixuType];
        sortBtn.selected = NO;
        [self changeBtnTitleAndImage:sortBtn Imgstr:@"diankai" TitStr:leftStr];
        
    }
    [self changeBtnTitleAndImage:btn Imgstr:imgStr TitStr:titStr];
    backView.hidden = !btn.selected;
    [_sortTableview reloadData];
}

- (void)changeBtnTitleAndImage:(UIButton *)btn Imgstr:(NSString *)imgStr TitStr:(NSString *)titStr
{
    [btn setImage:IMAGE(imgStr) forState:UIControlStateNormal];
    [btn setTitle:titStr forState:UIControlStateNormal];
    CGSize titleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
    CGSize imgSize = btn.currentImage.size;
    UIEdgeInsets   titleEdgeInsets = UIEdgeInsetsMake(0, -imgSize.width-5, 0, imgSize.width+5);
    UIEdgeInsets imgEdgeInsets = UIEdgeInsetsMake(0, 5+titleSize.width, 0, -titleSize.width-5);
    btn.titleEdgeInsets = titleEdgeInsets;
    btn.imageEdgeInsets = imgEdgeInsets;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 22)
    {
        if (_closeType == 0)
        {
            return _leftAry.count;
        }
        return _rightAry.count+1;
    }
    return _allGoodsAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        NSString *cellStr = cellPath;
        if (tableView.tag == 22)
        {
            cellStr = cellSortPath;
        }
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 11)
    {
        if (_allGoodsAry.count > 0)
        {
            ProductModel *productModel = _allGoodsAry[indexPath.row];
            NSArray *imgArray = productModel.productImgList;
            UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 80*displaywidth, 80*displaywidth) ImageName:@""];
            if (imgArray.count > 0)
            {
                ImagelistModel *imgModel = imgArray[0];
                [imgView sd_setImageWithURL:[NSURL URLWithString:imgModel.imgUrl]];
            }
            imgView.contentMode = UIViewContentModeScaleAspectFill;
            imgView.layer.masksToBounds = YES;
            [cell.contentView addSubview:imgView];
            UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 5*displaywidth, SCREEN_WIDTH-130*displaywidth, 35*displaywidth) Font:12.0 Text:productModel.productName Color:RGB(77, 77, 77)];
            titlab.lineBreakMode = NSLineBreakByTruncatingTail;
            titlab.adjustsFontSizeToFitWidth = NO;
            [cell.contentView addSubview:titlab];
//            UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"￥%@",productModel.price] Color:priceColor];
//            [cell.contentView addSubview:moneyLab];
//            UIButton *priceBtn = [ZCControl createButtonWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) ImageName:@"jifen" Target:nil Action:nil Title:string(@" ", productModel.price) Font:14.0];
//            priceBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//            priceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//            priceBtn.backgroundColor = [UIColor clearColor];
//            [priceBtn setTitleColor:priceColor forState:UIControlStateNormal];
//            [cell.contentView addSubview:priceBtn];
            UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(115*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%@ +",productModel.priceDescribe] Color:RGB(241, 90, 36)];
            CGRect abounds = [moneyLab.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-200*displaywidth, 20*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(12.0+fontScale)} context:nil];
            CGFloat width1 = abounds.size.width+20;
            moneyLab.frame = CGRectMake(115*displaywidth, 70*displaywidth, width1, 20*displaywidth);
            [cell.contentView addSubview:moneyLab];
            CGFloat width2 = 0;
           
            UILabel *integralLab = [ZCControl createLabelWithFrame:CGRectMake(width1+120*displaywidth, 70*displaywidth, 120*displaywidth, 20*displaywidth) Font:11.0 Text:[NSString stringWithFormat:@"%@积分",productModel.integral] Color:[UIColor whiteColor]];
            integralLab.textAlignment = NSTextAlignmentCenter;
            integralLab.layer.cornerRadius = 2;
            integralLab.layer.masksToBounds = YES;
            integralLab.backgroundColor = RGB(24, 159, 186);
            [cell.contentView addSubview:integralLab];
            CGRect bounds1 = [integralLab.text boundingRectWithSize:CGSizeMake(120*displaywidth, 20*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(11.0+fontScale)} context:nil];
            width2 = bounds1.size.width+10;
            if (bounds1.size.width > 120*displaywidth)
            {
                width2 = 120*displaywidth;
            }
            integralLab.frame = CGRectMake(width1+120*displaywidth, 70*displaywidth, width2, 20*displaywidth);
            
            
            UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(width1+width2+140*displaywidth, 70*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@" %@购买",productModel.productSales] Color:RGB(179, 179, 179)];
            [cell.contentView addSubview:numLab];
            
            UIView *lineFView = [[UIView alloc] initWithFrame:CGRectMake(0, 99*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
            lineFView.backgroundColor = LinesColor;
            [cell.contentView addSubview:lineFView];
            
            
        }
    }
    else
    {
        NSString *nameStr;
        UIColor *colors;
        BOOL hide = YES;
        if (_closeType == 1)
        {
            
            colors = BlaTiColor;
            if (indexPath.row == 0)
            {
                nameStr = @"全部分类";
            }
            else
            {
                if (_rightAry.count > 0)
                {
                    ProdTypeModel *model = _rightAry[indexPath.row - 1];
                    nameStr = model.typeName;
                }
                
            }
            if (_rightType == indexPath.row)
            {
                colors = ThemeColor;
                hide = NO;
            }
        }
        else
        {
            nameStr = _leftAry[indexPath.row];
            colors = BlaTiColor;
            if (_paixuType == indexPath.row)
            {
                colors = ThemeColor;
                hide = NO;
            }
        }
        cell.textLabel.text = nameStr;
        cell.textLabel.textColor = colors;
        cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
        UIImageView *closeImgview = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-50*displaywidth, 5*displaywidth, 20*displaywidth, 20*displaywidth) ImageName:@"gouprodu"];
        closeImgview.contentMode = UIViewContentModeScaleAspectFit;
        closeImgview.hidden = hide;
        [cell.contentView addSubview:closeImgview];
        UIView *lineBview = [[UIView alloc] initWithFrame:CGRectMake(0, 29*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineBview.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineBview];
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 22)
    {
        if (_closeType == 0)
        {
            _paixuType = indexPath.row;
            [self closeBtnAct:sortBtn];
            
        }
        else
        {
            _rightType = indexPath.row;
            if (indexPath.row == 0)
            {
                _proID = @"";
            }
            else
            {
                ProdTypeModel *model = _rightAry[indexPath.row - 1];
                _proID = s_str(model.ID);
            }
           [self closeBtnAct:salesBtn];
        }
        [self loadNewHeaderData];
        [tableView reloadData];
    }
    else
    {
        ProductModel *productModel = _allGoodsAry[indexPath.row];
        LiveGoodsDetailViewController *vc = [[LiveGoodsDetailViewController alloc] init];
        vc.idStr = s_str(productModel.ID);
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 22)
    {
        return 30*displaywidth;
    }
    return 100*displaywidth;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _allGoodsAry.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    CGFloat W = SCREEN_WIDTH/2;
    CGFloat X = 0;
    if (indexPath.row %2 == 1)
    {
        X = 7.5*displaywidth;
    }
    if (_allGoodsAry.count > 0)
    {
        cell.backgroundColor = BacksColor;
        ProductModel *productModel = _allGoodsAry[indexPath.row];
        NSArray *imgArray = productModel.productImgList;
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(X, 0, W-7.5*displaywidth, 240*displaywidth)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:whiteView];
        UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, W-7.5*displaywidth, 170*displaywidth)];
        if (imgArray.count > 0)
        {
            ImagelistModel *imgModel = imgArray[0];
            [imageView1 sd_setImageWithURL:[NSURL URLWithString:imgModel.imgUrl]];
        }
        imageView1.contentMode = UIViewContentModeScaleAspectFill;
        imageView1.layer.masksToBounds = YES;
        [whiteView addSubview:imageView1];
        UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 170*displaywidth, W-37.5*displaywidth, 25*displaywidth) Font:12.0 Text:productModel.productName Color:RGB(77, 77, 77)];
        titlab.lineBreakMode = NSLineBreakByTruncatingTail;
        titlab.adjustsFontSizeToFitWidth = NO;
        [cell.contentView addSubview:titlab];
//        UILabel *moneyLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 210*displaywidth, 100*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"￥%@",productModel.price] Color:priceColor];
//        [cell.contentView addSubview:moneyLab];
//        UIButton *priceBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 210*displaywidth, 100*displaywidth, 20*displaywidth) ImageName:@"jifen" Target:nil Action:nil Title:string(@" ", productModel.price) Font:14.0];
//        priceBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//        priceBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//        priceBtn.backgroundColor = [UIColor clearColor];
//        [priceBtn setTitleColor:priceColor forState:UIControlStateNormal];
//        [cell.contentView addSubview:priceBtn];
        UILabel *priceLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 200*displaywidth, W-37.5*displaywidth, 15*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"￥%.2lf元 +",[productModel.price floatValue]] Color:priceColor];
        [cell.contentView addSubview:priceLab];
        
        priceLab.frame = CGRectMake(15*displaywidth, 200*displaywidth, W-37.5*displaywidth, 15*displaywidth);
        UILabel *integralLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 217.5*displaywidth, (W-37.5*displaywidth)/2, 15*displaywidth) Font:11.0 Text:[NSString stringWithFormat:@"%@积分",productModel.integral] Color:[UIColor whiteColor]];
        integralLab.textAlignment = NSTextAlignmentCenter;
        integralLab.layer.cornerRadius = 2;
        integralLab.layer.masksToBounds = YES;
        integralLab.backgroundColor = RGB(24, 159, 186);
        [cell.contentView addSubview:integralLab];
        CGRect bounds1 = [integralLab.text boundingRectWithSize:CGSizeMake(W-37.5*displaywidth, 15*displaywidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:NormalFontWithSize(11.0+fontScale)} context:nil];
        CGFloat width1 = bounds1.size.width+10;
        if (bounds1.size.width > (W-37.5*displaywidth)/2)
        {
            width1 = (W-37.5*displaywidth)/2;
        }
        integralLab.frame = CGRectMake(15*displaywidth, 217.5*displaywidth, width1, 15*displaywidth);
        
        
        UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(100*displaywidth, 215*displaywidth, 80*displaywidth, 20*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@" %@购买",productModel.productSales] Color:RGB(179, 179, 179)];
        [cell.contentView addSubview:numLab];
        
        
    }
    
    
    return cell;
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH/2, 245*displaywidth);
}

//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //cell被电击后移动的动画
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
    ProductModel *productModel = _allGoodsAry[indexPath.row];
    LiveGoodsDetailViewController *vc = [[LiveGoodsDetailViewController alloc] init];
    vc.idStr = s_str(productModel.ID);
    [self.navigationController pushViewController:vc animated:YES];
    
}



- (void)requestLifeGoodsList:(NSString *)sortKey ProductTypeId:(NSString *)proID
{
    WEAKSELF;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    [dic setObject:_typeID forKey:@"productTypeParentId"];
    [dic setObject:s_Integer(_page) forKey:@"page"];
    if (![sortKey isEqualToString:@""])
    {
        [dic setObject:sortKey forKey:@"sort"];
    }
    if (![proID isEqualToString:@""])
    {
        [dic setObject:proID forKey:@"productTypeId"];

    }
    [HYBaseHandler requestLifeGoodsListParmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_footer endRefreshing];

        if ([obj server_Getcodecheck])
        {
            NSArray *goodsAry = [ProductModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (weakSelf.page == 0)
                {
                    [weakSelf.allGoodsAry removeAllObjects];
                }
                [weakSelf.allGoodsAry addObjectsFromArray:goodsAry];
                [weakSelf.tableview reloadData];
                [weakSelf.collectionView reloadData];
                
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
    }];

}

- (void)requestLifeproductType
{
    WEAKSELF;
    NSDictionary *dic = @{@"parentId":_typeID};
    [HYBaseHandler requestLifeproductTypeParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            weakSelf.rightAry = [ProdTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.sortTableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];

        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
